package cnsatmsmart.automation.mmi.model.data.TSafe;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author SIK - PTIK, BPPT
 **/

public class STAR { //based on TSafe
    /* atribut - atribut yang dimiliki oleh kelas STAR */
        private String ID;                          //ID
        private Route basic;                        //rute utama
        private Fix transitionFix;                  //titik transisi (fix pertama dari rute utama)
        private Map firstToRoute = new HashMap();   //pemetaan dari rute-rute yang diakhiri di transitionFix dan titik transisinya

    /* constructor - user defined ctor, no default ctor */
        // <editor-fold defaultstate="collapsed" desc="ctor">
        /*
         * @param = atribut
         */
        public STAR (String ID, Route basic) {
            this.ID = ID;
            this.basic = basic;
            this.transitionFix = this.basic.firstFix();
            this.firstToRoute.put(this.transitionFix, this.basic);
        }
        // </editor-fold>

    /* getter */
        public String getID () {
            /* mengembalikan nilai dari atribut ID */
            return (this.ID);
        }
        public Route getbasicRoute () {
            /* mengembalikan nilai dari atribut basic */
            return (this.basic);
        }

    /* method - method yang dimiliki oleh kelas STAR */
        public Route getFromRoute (Fix first) {
            return ((Route)this.firstToRoute.get(first));
        }

        public Set getAllRoutes () {
            return (new HashSet(this.firstToRoute.values()));
        }

        public void addTransition (Route transition) {
            if (! transition.lastFix().equals(this.transitionFix)) {
                throw new IllegalArgumentException(new StringBuilder().append("STAR ").append(this.ID).append(" : last fix of transition ").append(transition).append(" is not the first fix of the basic route").append(this.basic).toString());
            } else {
                Route fullRoute = appendTransition(transition);
                this.firstToRoute.put(fullRoute.firstFix(), fullRoute);
                if (! fullRoute.firstFix().equals(transition.firstFix())) {
                    throw new RuntimeException("transition appended wrong");
                } else {
                    return;
                }
            }
        }
        @SuppressWarnings("empty-statement")
        private Route appendTransition (Route transition) {
            Route fullRoute = new Route();

            Iterator fixIter;

            for (fixIter = transition.fixesIterator(); fixIter.hasNext(); fullRoute.addFix((Fix)fixIter.next()));

            fixIter = this.basic.fixesIterator();
            fixIter.next();

            for ( ; fixIter.hasNext(); fullRoute.addFix((Fix)fixIter.next()));

            return (fullRoute);
        }
}
